package com.china08.yxyapi.api.comm;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SubjectToIdController extends YxyApiBaseController {

    @Autowired
    private YxyResSubjectRepository yrsr;

    @ApiOperation(value = "获得学科ID")
    @RequestMapping(value = "/api_yw/comm/subject/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(@PathVariable(value = "name") String name) throws IOException {
        // byte[] by = Base64.decodeBase64(name);
        // name = new String(by, "utf-8");
        RespModel model = new RespModel();
        YxyResSubject subject = yrsr.findByName(name.trim());
        Assert.notNull(subject, "学科未找到请联系管理员");
        model.id = subject.getId();
        return model;
    }

    protected class RespModel extends YxyApiRespModel {
        public String id;
    }

}
